﻿using LeanApp.Bll;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// BalanceEquipmentChartCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class BalanceEquipmentChartCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="bll">线平衡分析业务</param>
        public BalanceEquipmentChartCtrl(string title, LineBalanceBll bll)
        {
            InitializeComponent();
            _title = title;
            _bll = bll;
            InitialChart();
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitialChart()
        {
            title.Text = _title;
            List<ISeries> series = new List<ISeries>();
            List<Axis> xAxises = new List<Axis>();
            List<Axis> yAxises = new List<Axis>();
            double yMax = 0;
            double yMin = 0;
            if (_bll != null && _bll.EquipmentBarData != null && _bll.EquipmentBarData.Count() > 0)
            {
                ColumnSeries<double> item = new ColumnSeries<double>()
                {
                    Values = _bll.EquipmentBarData.Values.ToArray(),
                    ScalesYAt = 0
                };
                series.Add(item);

                Axis xAxis = new Axis()
                {
                    Labels = _bll.EquipmentBarData.Keys.ToArray()
                };
                xAxises.Add(xAxis);
                yMax = _bll.EquipmentBarData.Values.Max() < yMax ? yMax : _bll.EquipmentBarData.Values.Max();
                yMin = _bll.EquipmentBarData.Values.Min() > yMin ? yMin : _bll.EquipmentBarData.Values.Min();
            }
            if (_bll != null && _bll.EquipmentTargetData != null && _bll.EquipmentTargetData.Count() > 0)
            {
                StepLineSeries<double> item = new StepLineSeries<double>()
                {
                    Values = _bll.EquipmentTargetData.Values.ToArray(),
                    ScalesYAt = 0
                };
                series.Add(item);
                yMax = _bll.EquipmentTargetData.Values.Max() < yMax ? yMax : _bll.EquipmentTargetData.Values.Max();
                yMin = _bll.EquipmentTargetData.Values.Min() > yMin ? yMin : _bll.EquipmentTargetData.Values.Min();
            }
            Axis yAxis = new Axis()
            {
                MinLimit = 1.05 * yMin,
                MaxLimit = 1.05 * yMax,
            };
            yAxises.Add(yAxis);
            chartCtrl.Series = series;
            chartCtrl.XAxes = xAxises;
            chartCtrl.YAxes = yAxises;
        }

        /// <summary>
        /// 线平衡业务分析
        /// </summary>
        private LineBalanceBll _bll = new LineBalanceBll();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;
    }
}
